<?php
/**
 *  菜单管理
 */
namespace App\Http\Controllers;
use App\Role;
use Illuminate\Http\Request;
use Request as Ruser;

class RolesController extends Controller
{
    public function __construct()
    {
        $this->middleware('check_role');
    }

    public function index(){
        $user = Ruser::user();
        return view('role.index',['user' => $user]);
    }

    public function manage(){
        $user = Ruser::user();
        return view('manage.index',['user' => $user]);
    }

    /**
     * 创建｜更新角色信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/5 9:33 上午
     * @流程:
     */
    public function save(Request $request) {
        $server = new \App\Services\RolesService();
        $result = $server->createOrUpdateRole($request);
        return $this->json($result);
    }


    /**
     * 获取角色列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/5 9:34 上午
     * @流程:
     */
    public function getList() {
        $result = Role::GetList();
        return $this->json($result);
    }

    /**
     * 更新角色状态 启用｜禁用
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/5 10:49 上午
     * @流程:
     */
    public function updateStatus(Request $request) {
        $data = $request->post();
        $result = Role::UpdateStatus($data);
        return $this->json($result);
    }

    /**
     * 更新工作人员状态 启用｜禁用
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/5 10:49 上午
     * @流程:
     */
    public function updateManageStatus(Request $request) {
        $data = $request->post();
        $result = Role::UpdateManageStatus($data);
        return $this->json($result);
    }

    /**
     * 删除角色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/5 11:13 上午
     * @流程:
     */
    public function delete(Request $request) {
        $id = $request->post('id');
        $result = Role::DelRole($id);
        return $this->json($result);
    }

    public function deleteManage(Request $request){
        $id = $request->post('id');
        $result = Role::DelRoleManage($id);
        return $this->json($result);
    }

    /**
     * 获取管理员列表
     * @return \Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/5 6:05 下午
     * @流程:
     */
    public function getManageList() {
        $server = new \App\Services\RolesService();
        $result = $server->GetManageList();
        return $this->json($result);
    }


    public function manageSave(Request $request) {
        $server = new \App\Services\RolesService();
        $result = $server->ManageSave($request);
        return $this->json($result);
    }

    protected function json($data, $status = 200) {
        return response()->json($data, $status, [], JSON_UNESCAPED_UNICODE);
    }
}